import { Injectable } from '@nestjs/common';
import { CreateArticleDto } from './dto/create-article.dto';
import {UpdateArticleDto} from './dto/update-article.dto'
import { PrismaService } from 'src/prisma/prisma.service';
import { PaginationDto } from './dto/pagination.dto';
import { CategoryService } from 'src/category/category.service';

@Injectable()
export class ArticleService {
  constructor(private readonly prisma: PrismaService,private readonly categoryService: CategoryService) {}
  // 创建
  create(createArticleDto: CreateArticleDto,) {
    return this.prisma.article.create({
      data: {
        title: createArticleDto.title,
        content: createArticleDto.content,
        category_id: +createArticleDto.categoryId,
      },
    });
  }
  // 查询所有（带分页）
  findAll(paginationDto?: PaginationDto) {
    const { page = 1, pageSize = 10 } = paginationDto || {};
    return this.prisma.article.findMany({
      skip: (page - 1) * pageSize, 
      take: +pageSize,
    });
  }
  // id查询
  findOne(id: number) {
    return this.prisma.article.findUnique({
      where: {

        id,

      },
    });
  }
  // 通过id更新
  update(id: number, updateArticleDto: UpdateArticleDto) {
    return this.prisma.article.update({
      where: {
        id,
      },
      data: {
        title: updateArticleDto.title,
        content: updateArticleDto.content,
        category_id: updateArticleDto.categoryId,
      },
    });
  }
  // 删除
  remove(id: number) {
    return this.prisma.article.delete({
      where: {
        id,
        },
        
  })
}
  // 通过分类id查询所有文章
 async  getArticleByCategoryId(categoryId: number) {
    const result =await this.prisma.article.findMany({
      where: {
        category_id: +categoryId,
      },
    });
    return result
  }
}
